#include "loginrequest.h"

LoginRequest::LoginRequest(QObject *parent)
    : QObject{parent}
{
    m_r=Request::getinstance();
    auto f = [&](){
      this->loginInfo.code=m_r->result.code;
      this->loginInfo.message=m_r->result.message;
      this->loginInfo.token=m_r->result.data;
      QJsonParseError error;
      QJsonDocument document = QJsonDocument::fromJson(m_r->result.data.toUtf8(), &error);
      if(true)
      {
          QVariantMap map = document.toVariant().toMap();
          if(map.contains("token"))
          {
              QString token = map["token"].toString();
              this->loginInfo.token=token;
          }
      }
      emit LoginByPasswordSuccess();
    };
    connect(m_r,&Request::sendLoginByPasswordSuccess,this,f);
}

LoginInfo LoginRequest::getLoginInfo(QString username, QString pwd)
{
    m_r->LoginByPassword(username,pwd);

    return this->loginInfo;
}
